local function musha_staminasleep_statusdisplays(self)
	if self.owner:HasTag("_musha") then
		local Musha_StaminaSleepBadge = require("widgets/musha_staminasleepbadge")
		self.musha_staminasleep = self:AddChild(Musha_StaminaSleepBadge(self.owner))
		self.owner.Musha_StaminaSleepBadge = self.musha_staminasleep

		function self:Setmusha_staminasleepPercent(pctold, pctnew)
			self.musha_staminasleep:SetPercent(pctnew, self.owner.replica.musha_staminasleep:Max())
		end

		function self:musha_staminasleepDelta(data)
			self:Setmusha_staminasleepPercent(data.oldpercent, data.newpercent)
		end

	--绕过玄学解决法
	--[[ self.owner:DoTaskInTime(
			0,
			function(owner, self)
				self.musha_staminasleep:SetPercent(1, self.owner.replica.musha_staminasleep:Max())
			end,
			self
		) ]]
	end
end

AddClassPostConstruct("widgets/statusdisplays", musha_staminasleep_statusdisplays)

local function musha_fatiguesleep_statusdisplays(self)
	if self.owner:HasTag("_musha") then
		local Musha_FatigueSleepBadge = require("widgets/musha_fatiguesleepbadge")
		self.musha_fatiguesleep = self:AddChild(Musha_FatigueSleepBadge(self.owner))
		self.owner.Musha_FatigueSleepBadge = self.musha_fatiguesleep

		function self:Setmusha_fatiguesleepPercent(pctold, pctnew)
			self.musha_fatiguesleep:SetPercent(pctnew, self.owner.replica.musha_fatiguesleep:Max())
		end

		function self:musha_fatiguesleepDelta(data)
			self:Setmusha_fatiguesleepPercent(data.oldpercent, data.newpercent)
		end
	end
end

AddClassPostConstruct("widgets/statusdisplays", musha_fatiguesleep_statusdisplays)

local function Spellpower_statusdisplays(self)
	if self.owner:HasTag("_musha") or self.owner:HasTag("musha_spellpower") then
		local musha_spellpowerBadge = require("widgets/musha_spellpowerbadge")
		self.musha_spellpower = self:AddChild(musha_spellpowerBadge(self.owner))
		self.owner.spellpowerbadge = self.musha_spellpower

		function self:SetSpellpowerPercent(pctold, pctnew)
			self.musha_spellpower:SetPercent(pctnew, self.owner.replica.musha_spellpower:Max())
		end

		function self:SpellpowerDelta(data)
			self:SetSpellpowerPercent(data.oldpercent, data.newpercent)
		end
	end
end

AddClassPostConstruct("widgets/statusdisplays", Spellpower_statusdisplays)

local function OnSetGhostMode(inst, self)
	self.modetask = nil

	if self.onmusha_staminasleepdelta ~= nil then
		self.inst:RemoveEventCallback("musha_staminasleepdelta", self.onmusha_staminasleepdelta, self.owner)
		self.onmusha_staminasleepdelta = nil
	end

	if self.onmusha_fatiguesleepdelta ~= nil then
		self.inst:RemoveEventCallback("musha_fatiguesleepdelta", self.onmusha_fatiguesleepdelta, self.owner)
		self.onmusha_fatiguesleepdelta = nil
	end

	if self.onspellpowerdelta ~= nil then
		self.inst:RemoveEventCallback("musha_spellpowerdelta", self.onspellpowerdelta, self.owner)
		self.onspellpowerdelta = nil
	end
end

local function OnSetPlayerMode(inst, self)
	if self.onmusha_staminasleepdelta == nil then
		self.onmusha_staminasleepdelta = function(owner, data)
			self:musha_staminasleepDelta(data)
		end
		self.inst:ListenForEvent("musha_staminasleepdelta", self.onmusha_staminasleepdelta, self.owner)
		if self.owner.replica.musha_staminasleep ~= nil then
			self:Setmusha_staminasleepPercent(0, self.owner.replica.musha_staminasleep:GetPercent())
		end
	end

	if self.onmusha_fatiguesleepdelta == nil then
		self.onmusha_fatiguesleepdelta = function(owner, data)
			self:musha_fatiguesleepDelta(data)
		end
		self.inst:ListenForEvent("musha_fatiguesleepdelta", self.onmusha_fatiguesleepdelta, self.owner)
		if self.owner.replica.musha_fatiguesleep ~= nil then
			self:Setmusha_fatiguesleepPercent(0, self.owner.replica.musha_fatiguesleep:GetPercent())
		end
	end

	if self.onspellpowerdelta == nil then
		self.onspellpowerdelta = function(owner, data)
			self:SpellpowerDelta(data)
		end
		self.inst:ListenForEvent("musha_spellpowerdelta", self.onspellpowerdelta, self.owner)
		if self.owner.replica.musha_spellpower ~= nil then
			self:SetSpellpowerPercent(0, self.owner.replica.musha_spellpower:GetPercent())
		end
	end
end

local function musha_statusdisplays_common(self)
	if self.owner:HasTag("_musha") then
		self.isghostmode_musha = true

		local SetGhostMode_old = self.SetGhostMode
		self.SetGhostMode = function(self, ghostmode)
			if not self.isghostmode_musha == not ghostmode then
			elseif ghostmode then
				self.isghostmode_musha = true
				self.musha_staminasleep:Hide()
				self.musha_fatiguesleep:Hide()
				self.musha_spellpower:Hide()
			else
				self.isghostmode_musha = nil
				self.musha_staminasleep:Show()
				self.musha_fatiguesleep:Show()
				self.musha_spellpower:Show()
			end
			if self.modetask_musha ~= nil then
				self.modetask_musha:Cancel()
			end
			self.modetask_musha = self.inst:DoTaskInTime(0, ghostmode and OnSetGhostMode or OnSetPlayerMode, self)
			SetGhostMode_old(self, ghostmode)
		end

		self:SetGhostMode(false)

		local badge_brain = self.brain:GetPosition()
		local badge_boatmeter = self.boatmeter:GetPosition()
		local badge_moisturemeter = self.moisturemeter:GetPosition()

		local AlwaysOnStatus = false
		for k, v in ipairs(KnownModIndex:GetModsToLoad()) do
			local Mod = KnownModIndex:GetModInfo(v).name
			if Mod == "Combined Status" then
				AlwaysOnStatus = true
			end
		end
		self.owner:DoTaskInTime(
			0,
			function(owner)
			end
		)
		if AlwaysOnStatus then
			if self.owner.musha_settings.hud == 1 then
				self.musha_staminasleep:SetPosition(badge_boatmeter.x, badge_boatmeter.y, 0)
				self.musha_fatiguesleep:SetPosition(badge_boatmeter.x, badge_boatmeter.y, 0)
				self.musha_spellpower:SetPosition(badge_moisturemeter.x, badge_moisturemeter.y, 0)
				self.boatmeter:SetPosition(-124, 35, 0)
				self.moisturemeter:SetPosition(-125, -52, 0)
			elseif self.owner.musha_settings.hud == 2 then
				self.musha_staminasleep:SetPosition(-115, -15, 0)
				self.musha_fatiguesleep:SetPosition(-115, -15, 0)
				self.musha_spellpower:SetPosition(-62, -52, 0)
				self.boatmeter:SetPosition(badge_boatmeter.x, badge_boatmeter.y - 83, 0)
			end
		else
			self.musha_staminasleep:SetPosition(-100, -15, 0)
			self.musha_fatiguesleep:SetPosition(-100, -15, 0)
			self.musha_spellpower:SetPosition(-40, -50, 0)
			self.brain:SetPosition(badge_brain.x + 40, badge_brain.y - 10, 0)
			self.boatmeter:SetPosition(badge_boatmeter.x + 40, badge_boatmeter.y - 80, 0)
			self.moisturemeter:SetPosition(badge_moisturemeter.x + 40, badge_moisturemeter.y - 5, 0)
		end
	end
end

AddClassPostConstruct("widgets/statusdisplays", musha_statusdisplays_common)
